<template>
  <div class="default-column">
    <div class="edit-column">
      <el-button type="primary" icon="el-icon-plus" circle @click="addConsumer" size="small"></el-button>
    </div>

    <el-table v-loading="customerListLoading" element-loading-text="拼命加载中"
              :data="customerList" border stripe class="default-table" size="mini">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="center" prop="id" label="客服ID"></el-table-column>
      <el-table-column align="center" prop="name" label="姓名"></el-table-column>
      <el-table-column align="center" prop="memo" label="备注"></el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="mini" type="success">在职</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="mini" type="info">离职</el-tag>
          <el-tag v-else size="mini" type="warning">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" style="color: #1E90FF" @click="updateConsumer(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" style="color: red" @click="deleteConsumer(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="page.current"
        :page-size="page.size"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>

    <!--新增/更新客服人员框-->
    <el-dialog :title=dialogTitle :visible.sync="consumerDialogVisible" width="40%" :before-close="handleClose">
      <el-form ref="form" :model="addConsumerInfo" label-width="80px" :rules="rules">
        <el-form-item label="客服ID" prop="id">
          <el-input v-if="dialogTitle === addConsumerDialogTitle" v-model="addConsumerInfo.id"></el-input>
          <el-input v-if="dialogTitle === updateConsumerDialogTitle" v-model="addConsumerInfo.id" disabled></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input v-model="addConsumerInfo.name"></el-input>
        </el-form-item>

        <el-form-item label="初始密码" prop="password">
          <el-input v-model="addConsumerInfo.password"></el-input>
        </el-form-item>

        <el-form-item label="备注">
          <el-input v-model="addConsumerInfo.memo"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="consumerDialogVisible = false;">取 消</el-button>
        <el-button type="primary" @click="clickConsumer('form')">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    components: {},
    name: 'Customer',
    data () {
      return {
        rules: {
          id: [
            { required: true, message: '请输入客服ID', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' },
            { pattern: '^[0-9]{2,5}$', message: '客服ID只能为数字' }
          ],
          name: [
            { required: true, message: '请输入客服名字', trigger: 'blur' },
            { pattern: '^[\u4e00-\u9fa5]{2,4}$', message: '客服名字只能是2-4个汉字' }
          ],
          password: [
            { required: true, message: '请输入初始密码', trigger: 'blur' }
          ]
        },

        dialogTitle: '',
        addConsumerDialogTitle: '新增客服人员',
        updateConsumerDialogTitle: '更新客服人员',
        consumerDialogVisible: false,
        addConsumerInfo: {
          id: 0,
          name: '',
          status: '',
          password: '',
          memo: '',
        },

        customerList: [],
        customerListLoading: false,
        page: {
          current: 1,
          size: 10,
          total: 0
        }
      }
    },
    created() {
      this.loadCustomerList();
    },
    methods: {
      // 页面参数修改
      handleSizeChange(val) {
        this.page.current = 1;
        this.page.size = val;
        this.loadCustomerList();
      },
      handleCurrentChange() {
        this.loadCustomerList();
      },

      // 客服人员信息更新
      addConsumer() {
        this.addConsumerInfo = {};
        this.dialogTitle = this.addConsumerDialogTitle;
        this.consumerDialogVisible = true;
      },
      clickConsumer(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            this.clickConsumerInternal();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      async clickConsumerInternal() {
        if (this.dialogTitle === this.addConsumerDialogTitle) {
          let data = await this.$$post('/customer', {
            serviceId:     this.addConsumerInfo.id,
            serviceName:   this.addConsumerInfo.name,
            password: this.addConsumerInfo.password,
            memo:   this.addConsumerInfo.memo
          })
          if (this.checkResponse(data)) {
            this.$message.success("添加客服 " + this.addConsumerInfo.name +" 成功");
          }
        } else if (this.dialogTitle === this.updateConsumerDialogTitle) {
          let data = await this.$$put('/customer', {
            serviceId:     this.addConsumerInfo.id,
            serviceName:   this.addConsumerInfo.name,
            status: this.addConsumerInfo.status,
            memo:   this.addConsumerInfo.memo
          })
          if (this.checkResponse(data)) {
            this.$message.success("更新客服 " + this.addConsumerInfo.name +" 成功");
          }
        }

        this.addConsumerInfo = {};
        this.loadCustomerList();
        this.consumerDialogVisible = false;
      },
      updateConsumer(consumer) {
        this.addConsumerInfo = consumer;
        this.dialogTitle = this.updateConsumerDialogTitle;
        this.consumerDialogVisible = true;
      },
      deleteConsumer(consumer) {
        this.$confirm('此操作将删除客服 ' + consumer.name +  ' , 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteConsumerInternal(consumer);
        }).catch(() => { });
      },
      async deleteConsumerInternal(consumer) {
        let data = await this.$$put('/customer/delete', {
          serviceId: consumer.id,
          serviceName: consumer.name,
          memo: consumer.memo,
          status: consumer.status
        })
        if (this.checkResponse(data)) {
          this.$message.success('删除成功!');
          this.loadCustomerList();
        }
      },

      // 加载客服人员列表
      async loadCustomerList() {
        this.customerListLoading = true;
        let data = await this.$$post('/customer/page/list', this.page)
        this.customerListLoading = false;
        if (this.checkResponse(data)) {
          data = data.data.data;
          this.page.total = data.total;
          this.customerList = data.data;
        }
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
